package smartclass.bussiness;

import android.annotation.SuppressLint;
import java.util.ArrayList;
import java.util.HashMap;

import smartclass.dao.DocumentDataDao;
import smartclass.entity.DocumentInfo;

public class DocumentBussiness {

	private static DocumentBussiness documentBussiness;
	private static DocumentDataDao documentDataDao;
	private ArrayList<DocumentInfo> arrayDocument;
	public HashMap<Integer, Boolean> checkExpandList;

	@SuppressLint("UseSparseArrays")
	public DocumentBussiness() {
		// TODO Auto-generated constructor stub
		documentDataDao = DocumentDataDao.getInstance();
		checkExpandList = new HashMap<Integer, Boolean>();
	}

	public static DocumentBussiness getInstance() {
		if (documentBussiness == null) {
			documentBussiness = new DocumentBussiness();
		}
		return documentBussiness;
	}

	public boolean addDocument(DocumentInfo doc) {
		return documentDataDao.addDocument(doc);
	}

	public DocumentInfo getDocument(int id) {
		return documentDataDao.getDocument(id);
	}

	public ArrayList<DocumentInfo> getAllDocument(int idTopic) {
		arrayDocument = new ArrayList<DocumentInfo>();
		for (DocumentInfo document : documentDataDao.getAllDocument()) {
			if (document.getIdTopic() == idTopic) {
				arrayDocument.add(document);
			}
		}
		return arrayDocument;
	}

	public boolean checkNameAvailable(String name) {
		boolean check = true;
		for (DocumentInfo doc : documentDataDao.getAllDocument()) {
			if (doc.getName().equals(name.trim())) {
				check = false;
			}
		}
		return check;
	}

	public boolean deleteDocument(DocumentInfo doc) {
		return documentDataDao.deleteDirection(doc.getId());
	}

	public boolean deleteArrayDocument(int idTopic) {
		for (DocumentInfo doc : getAllDocument(idTopic)) {
			deleteDocument(doc);
		}
		return true;
	}

	public void initValuesCheckListExpand(int size) {
		for (int i = 0; i < size; i++) {
			checkExpandList.put(i, false);
		}
	}

	public void updateHashMapCheckListExpand(int groupPosition, boolean check) {
		for (int i = 0; i < checkExpandList.size(); i++) {
			if (checkExpandList.containsKey(groupPosition)) {
				checkExpandList.remove(groupPosition);
				checkExpandList.put(groupPosition, check);
			} else {
				checkExpandList.put(groupPosition, check);
			}
		}
	}

	public boolean getCheckExpandValues(int groupPosition) {
		if (checkExpandList.get(groupPosition) != null) {
			return checkExpandList.get(groupPosition);
		} else {
			return false;
		}
	}

}
